﻿/*
 * User: Masy Freddy
 * Date: 19/01/2012
 * Time: 6:43
 */
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;

using MetaVideoEditorEvolved.AddIn.AddInViews;
using MetaVideoEditorEvolved.Helper;

namespace SaverMtn
{
	[System.AddIn.AddIn("Mtn Saver",
	                    Version = "1.0.0.0",
	                    Description = "Mtn Saver",
	                    Publisher = "Masy Freddy")]
	public class SaveMtn : SaverView
	{
		private ConfigurationAddIn configurationAddIn;
		
		public SaveMtn()
		{
			configurationAddIn = ConfigurationManager.Deserialize();
			
			if(configurationAddIn == null)
			{
				configurationAddIn = new ConfigurationAddIn()
				{
					TraceLevel = "Error",
					PercentCapture = "0.2d"
				};
				
				ConfigurationManager.Serialize(configurationAddIn);
			}
		}
		
		public override void ShowOption()
		{
			new Option(configurationAddIn).ShowDialog();
		}
		
		public override void SaveSerie(string mediaLocation, MetaVideoEditorEvolved.AddIn.Entity.Serie serie)
		{
		}
		
		public override void SaveSeason(string mediaLocation, MetaVideoEditorEvolved.AddIn.Entity.Season season)
		{
		}
		
		public override void SaveMovie(string mediaLocation, MetaVideoEditorEvolved.AddIn.Entity.Movie movie)
		{
			ExtractMtn(Path.Combine(mediaLocation,new FileInfo(movie.FullPath).Name), true);
		}
		
		public override void SaveEpisode(string mediaLocation, MetaVideoEditorEvolved.AddIn.Entity.Episode episode)
		{
			ExtractMtn(mediaLocation, false);
		}
		
		public override bool HasOption()
		{
			return true;
		}
		
		private void ExtractMtn(string mediaLocation, bool isMovie)
		{
			try
			{
				Tracer.TracerInstance.TraceInformation("Start the extraction mtn for medialocation:" + mediaLocation);
				
				DirectoryInfo di = new DirectoryInfo(mediaLocation);
				
				if(File.Exists(mediaLocation))
				{
					di = di.Parent;
				}
				
				var frame = JockerSoft.Media.FrameGrabber.GetFrameFromVideo(mediaLocation, double.Parse(configurationAddIn.PercentCapture));
				
				if(configurationAddIn.IsMediaBrowser)
				{
					var fileName = string.Empty;
					
					if(isMovie)
					{
						fileName = Path.Combine(di.FullName, "folder.jpg");
					}
					else
					{
						var fi = new FileInfo(mediaLocation);
						fileName = Path.Combine(di.FullName, "metadata", fi.Name.Replace(fi.Extension, string.Empty) + ".jpg");
					}
					
					if(File.Exists(fileName) && configurationAddIn.CanDeleteFile)
					{
						File.Delete(fileName);
					}
					
					if(!File.Exists(fileName))
					{
						frame.Save(fileName, ImageFormat.Jpeg);
						Tracer.TracerInstance.TraceInformation("Extracted the mtn for mediabrowser");
					}
				}
				
				if(configurationAddIn.IsXBMC)
				{
					var fileName = string.Empty;
					
					if(isMovie)
					{
						if(configurationAddIn.IsXBMCSavePosterMediaName)
						{
							var fi = new FileInfo(mediaLocation);
							fileName = Path.Combine(di.FullName, fi.Name.Replace(fi.Extension, ".tbn") + ".tbn");
						}
						else
						{
							fileName = Path.Combine(di.FullName, "movie.tbn");
						}
					}
					else
					{
						var fi = new FileInfo(mediaLocation);
						fileName = Path.Combine(di.FullName, fi.Name.Replace(fi.Extension, string.Empty) + ".tbn");
					}
					
					if(File.Exists(fileName) && configurationAddIn.CanDeleteFile)
					{
						File.Delete(fileName);
					}
					
					if(!File.Exists(fileName))
					{
						frame.Save(fileName, ImageFormat.Jpeg);
						Tracer.TracerInstance.TraceInformation("Extracted the mtn for xbmc");
					}
				}
			}
			catch(Exception ex)
			{
				MetaVideoEditorEvolved.Helper.Tracer.TracerInstance.TraceError("Error in the extraction of mtn for medialocation:" + mediaLocation, ex);
			}
		}
	}
}